#-*- coding: utf-8 -*-

import time
import threading
import MySQLdb

import GetUserList
from Header import ItemHeader


ITEM_SELECT = """
	SELECT * FROM info_item WHERE character_seq = %d AND bag_type = %d AND status = 0
"""

ITEM_UPDATE = """
    UPDATE info_item
        SET status = %d,
        character_seq = %d, bag_type = %d, bag_slot_number = %d,
        stack_num = %d, class_type = %d, sub_type = %d,
        tid = %d, iconidx = %d, item_type = %d,
        cur_duration = %d, max_duration = %d, set_type = %d,
        non_identity = %d, cur_refine_step = %d, quality = %d,
        level = %d, eff_type0 = %d, eff_pos0 = %d,
        eff_value0 = %d, eff_type1 = %d, eff_pos1 = %d,
        eff_value1 = %d, eff_type2 = %d, eff_pos2 = %d,
        eff_value2 = %d, eff_type3 = %d, eff_pos3 = %d,
        eff_value3 = %d, eff_type4 = %d, eff_pos4 = %d,
        eff_value4 = %d, eff_type5 = %d, eff_pos5 = %d,
        eff_value5 = %d, eff_type6 = %d, eff_pos6 = %d,
        eff_value6 = %d, open_upgrade_stone_slot = %d, ablity_enhance_rate = %d,
        max_enhance_step = %d, buy_use = %d, evolve_step = %d,
        evolve_max = %d, evolve_point = %d, evolve_eff = %d,
        evolve_eff_value = %d, reg_date = %d
    WHERE seq = %d
"""

MAIL_SELECT = """
	SELECT * FROM data_mail WHERE recv_user_seq = %d AND mail_status != 2 AND send_date >= %d
"""

class MySQLStress(threading.Thread) :
	def __init__(self, ip, type, user_count, start_pos) :
		threading.Thread.__init__(self)

		self.conn = MySQLdb.connect(ip, "gamevil", "vlwlrpaqlf", "pz_user")
		self.csr  = self.conn.cursor()

		self.ip			= ip
		self.type		= type
		self.start_pos	= start_pos
		self.user_count = user_count

		self.shutdown	= False

	def __del__(self) :
		try : self.csr.close()
		except : pass

		try : self.conn.close()
		except : pass

	def info_item(self) :
		print "th(%s) start_pos (%d)" % (self.name, self.start_pos)
		characterseq_list = GetUserList.GetCharacterSeqFromInfoItem(self.ip, self.start_pos, self.user_count)

		item_list = list()

		for character_seq in characterseq_list :
			for bag_type in (0, 1, 2, 5, 6) :
				self.csr.execute(ITEM_SELECT % (character_seq, bag_type))
			#	print "character_seq(%d) bag_type(%d) cnt(%d)" % (character_seq, bag_type, self.csr.rowcount)
				for res in self.csr.fetchall() :
					item_list.append(res)

		print "th(%s) item_count(%d)" % (self.name, len(item_list))

		while (not self.shutdown) :
			for item in item_list :
				if (self.shutdown) : break;
				self.csr.execute(ITEM_UPDATE % (item[ItemHeader.status], item[ItemHeader.character_seq], item[ItemHeader.bag_type],           \
										item[ItemHeader.bag_slot_number], item[ItemHeader.stack_num], item[ItemHeader.class_type],     \
										item[ItemHeader.sub_type], item[ItemHeader.tid], item[ItemHeader.iconidx],                     \
										item[ItemHeader.item_type], item[ItemHeader.cur_duration], item[ItemHeader.max_duration],      \
										item[ItemHeader.set_type], item[ItemHeader.non_identity], item[ItemHeader.cur_refine_step],    \
										item[ItemHeader.quality], item[ItemHeader.level], item[ItemHeader.eff_type0],                  \
										item[ItemHeader.eff_pos0], item[ItemHeader.eff_value0], item[ItemHeader.eff_type1],            \
										item[ItemHeader.eff_pos1], item[ItemHeader.eff_value1], item[ItemHeader.eff_type2],            \
										item[ItemHeader.eff_pos2], item[ItemHeader.eff_value2], item[ItemHeader.eff_type3],            \
										item[ItemHeader.eff_pos3], item[ItemHeader.eff_value3], item[ItemHeader.eff_type4],            \
										item[ItemHeader.eff_pos4], item[ItemHeader.eff_value4], item[ItemHeader.eff_type5],            \
										item[ItemHeader.eff_pos5], item[ItemHeader.eff_value5], item[ItemHeader.eff_type6],            \
										item[ItemHeader.eff_pos6], item[ItemHeader.eff_value6], item[ItemHeader.open_upgrade_stone_slot],  \
										item[ItemHeader.ablity_enhance_rate], item[ItemHeader.max_enhance_step], item[ItemHeader.buy_use], \
										item[ItemHeader.evolve_step], item[ItemHeader.evolve_max], item[ItemHeader.evolve_point],  \
										item[ItemHeader.evolve_eff], item[ItemHeader.evolve_eff_value], item[ItemHeader.reg_date], \
										item[ItemHeader.seq])
								)
				self.conn.commit()
				



	def data_mail(self) :
		userseq_list = GetUserList.GetUserSeqFromDataMail(self.ip, self.start_pos, self.user_count)

		three_day_ago = int(time.time() - (3 * 3600 * 24))

		while (not self.shutdown) :
			for user_seq in userseq_list :
				if (self.shutdown) : break
				self.csr.execute(MAIL_SELECT % (user_seq, three_day_ago))
				self.csr.fetchall()
			#	print "user_seq(%d) cnt(%d)" % (user_seq, self.csr.rowcount)

			time.sleep(1)
			three_day_ago = int(time.time() - (3 * 3600 * 24))



	def data_news(self) :
		userseq_list = GetUserList.GetUserSeqFromDataNews(self.ip, self.start_pos, self.user_count)

		while (not self.shutdown) :
			for user_seq in userseq_list :
				if (self.shutdown) : break
				self.csr.callproc("sp_select_data_news", (user_seq, 1))
				self.csr.fetchall()
			#	print "user_seq(%d) cnt(%d)" % (user_seq, self.csr.rowcount)

				try : self.csr.close()
				except : pass

				self.csr = self.conn.cursor()

			time.sleep(1)





	def run(self) :
		if (self.type == "item") : self.info_item()
		if (self.type == "mail") : self.data_mail()
		if (self.type == "news") : self.data_news()
